home *** CD-ROM | disk | FTP | other *** search
/ Practical Algorithms for Image Analysis / Practical Algorithms for Image Analysis.iso / CH_6.3 / XSGT / GEN_DT_I.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-11  |  4.0 KB  |  205 lines

  1. /* 
  2.  * gen_dt_i.c
  3.  * 
  4.  * Practical Algorithms for Image Analysis
  5.  * 
  6.  * Copyright (c) 1997, 1998, 1999 MLMSoftwareGroup, LLC
  7.  */
  8.  
  9. /*
  10.  * GEN_DT_IO
  11.  * 
  12.  * generic modules to read data file of type .adt, .pdt, etc 
  13.  * and write data file of type *.aft (same as *.pft) for plotting 
  14.  *
  15.  */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18.  
  19. #undef    DEBUG
  20.  
  21.  
  22. #define    STAT_WT        1.0
  23.  
  24.  
  25.  
  26.  
  27. /*
  28.  * read first line in data file to determine size of record
  29.  * (expects file of type .pdt, .adt, .azv, etc)
  30.  */
  31. int
  32. get_record_size (file)
  33.      FILE *file;
  34. {
  35.   int retval, n_rec;
  36.  
  37.   if (((retval = fscanf (file, "%d ", &n_rec)) == 0) || (retval == EOF)) {
  38.     printf ("wrong input file format!\n");
  39.     exit (1);
  40.   }
  41.   return (n_rec);
  42. }
  43.  
  44.  
  45. int
  46. get_prm_size (file)
  47.      FILE *file;
  48. {
  49.   int retval, n_rec;
  50.  
  51.   if (((retval = fscanf (file, "%d ", &n_rec)) == 0) || (retval == EOF)) {
  52.     printf ("wrong input file format!\n");
  53.     exit (1);
  54.   }
  55.   return (n_rec);
  56. }
  57.  
  58.  
  59.  
  60. /*
  61.  * read data from file of type .pdt
  62.  */
  63. void
  64. get_col2_data (file, n_parms, a, npts, x, y, sig)
  65.      FILE *file;
  66.      int n_parms, npts;
  67.      float *a, *x, *y, *sig;
  68. {
  69.   int i;
  70.   int retval;
  71.  
  72. /*
  73.  * first, read starting values for fit parameters
  74.  */
  75.   for (i = 0; i < n_parms; i++) {
  76.     if (((retval = fscanf (file, "%f", (a + i))) == 0) || (retval == EOF)) {
  77.       printf ("wrong input file format for parameters!\n");
  78.       exit (1);
  79.     }
  80. #ifdef DEBUG
  81.     printf ("...a[%d] = %f\n", i, *(a + i));
  82. #endif
  83.   }
  84.  
  85. /*
  86.  * now read data;
  87.  * -->note: mrqmin expects unit offset arrays
  88.  */
  89.   for (i = 0; i < npts; i++) {
  90.     if ((retval = fscanf (file, "%f %f", (x + i), (y + i)) == 0) || (retval == EOF)) {
  91.       printf ("wrong input file format for data points!\n");
  92.       exit (1);
  93.     }
  94.     *(sig + i) = (float) STAT_WT;
  95. #ifdef DEBUG
  96.     printf ("...x[%d] = %f, y[%d] = %f\n", i, *(x + i), i, *(y + i));
  97. #endif
  98.   }
  99. }
  100.  
  101.  
  102. /*
  103.  * read data from file of type .adt, .azv
  104.  */
  105. void
  106. get_col3_data (file, n_parms, a, npts, x, oy, y, sig)
  107.      FILE *file;
  108.      int n_parms, npts;
  109.      float *a, *x, *oy, *y, *sig;
  110. {
  111.   int i;
  112.   int retval;
  113.  
  114. /*
  115.  * first, read starting values for fit parameters
  116.  */
  117.   for (i = 0; i < n_parms; i++) {
  118.     if (((retval = fscanf (file, "%f", (a + i))) == 0) || (retval == EOF)) {
  119.       printf ("wrong input file format for parameters!\n");
  120.       exit (1);
  121.     }
  122. #ifdef DEBUG
  123.     printf ("...a[%d] = %f\n", i, *(a + i));
  124. #endif
  125.   }
  126.  
  127. /*
  128.  * now read data;
  129.  * -->note: mrqmin expects unit offset arrays
  130.  */
  131.   for (i = 0; i < npts; i++) {
  132.     if ((retval = fscanf (file, "%f %f %f", (x + i), (oy + i), (y + i)) == 0) || (retval == EOF)) {
  133.       printf ("wrong input file format for data points!\n");
  134.       exit (1);
  135.     }
  136.     *(sig + i) = (float) STAT_WT;
  137. #ifdef DEBUG
  138.     printf ("\n...x[%d] = %f, oy[%d] = %f, y[%d] = %f\n",
  139.             i, *(x + i), i, *(oy + i), i, *(y + i));
  140. #endif
  141.   }
  142. }
  143.  
  144.  
  145.  
  146.  
  147. /*
  148.  * write .adt file (to be read by acff.c)
  149.  * same as .azv, .azd
  150.  */
  151. void
  152. write_adt_file (file, x_data, y_data, acf, n_pts, n_parms)
  153.      FILE *file;
  154.      float *x_data, *y_data, *acf;
  155.      int n_pts, n_parms;
  156. {
  157.   float dummy = (float) 1.0;
  158.   int i;
  159.  
  160.   fprintf (file, "%d %d\n", n_pts, n_parms);
  161.  
  162.   for (i = 0; i < n_parms; i++)
  163.     fprintf (file, "%f\n", dummy);
  164.  
  165.   for (i = 0; i < n_pts; i++)
  166.     fprintf (file, "%f %f %f\n", *(x_data + i), *(y_data + i), *(acf + i));
  167.  
  168.   fprintf (file, "\n");
  169.  
  170. }
  171.  
  172.  
  173. /*
  174.  * write data file of type .pft for plotting by pwr_plt.c
  175.  */
  176. void
  177. write_pft_file (file, x, y, rsd, n_pts, x_fit, y_fit, n_fit,
  178.                 parameters, n_parms, chisq)
  179.      FILE *file;
  180.      float *x, *y, *rsd;
  181.      float *x_fit, *y_fit;
  182.      float *parameters;
  183.      double chisq;
  184.      int n_pts, n_fit, n_parms;
  185. {
  186.   int i;
  187.  
  188.  
  189.   fprintf (file, "%d %d %d\n", n_pts, n_fit, n_parms);
  190.  
  191.   for (i = 0; i < n_pts; i++)
  192.     fprintf (file, "%f %f %f\n", *(x + i), *(y + i), *(rsd + i));
  193.  
  194.   for (i = 0; i < n_fit; i++)
  195.     fprintf (file, "%f %f\n", *(x_fit + i), *(y_fit + i));
  196.  
  197.   fprintf (file, "%f\n", chisq);
  198.  
  199.   for (i = 0; i < n_parms; i++)
  200.     fprintf (file, "%f\n", *(parameters + i));
  201.  
  202.   fprintf (file, "\n");
  203.  
  204. }
  205.